package cn.wp.org.controller;

import cn.wp.basic.util.AjaxResult;
import cn.wp.basic.util.PageList;
import cn.wp.org.domain.Department;
import cn.wp.org.query.DepartmentQuery;
import cn.wp.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 根据id查询一个对象
     * @param id
     * @return
     */
    @ApiOperation(value = "查询一个部门对象",notes = "根据id查询")
    @GetMapping("/{id}")
    public Department findOne (@PathVariable("id") Long id){
        return departmentService.findOne(id);
    }

    /**
     * 查询所有对象
     * @return
     */
    @ApiOperation(value = "查询所有部门对象")
    @GetMapping
    public List<Department> findAll (){
        return departmentService.findAll();
    }

    /**
     * 加载部门一级部门和所属的二级部门
     * @return
     */
    @ApiOperation(value = "加载部门树")
    @GetMapping("/deptTree")
    public List<Department> getDeptTree(){
        return departmentService.getDeptTree();
    }

    /**
     * 新增和修改
     * @param department
     * @return
     */
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.add(department);
            } else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！");
        }
    }

    /**
     * 分页
     * @param departmentQuery
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    /**
     * 删除一个对象
     * @param id
     * @return
     */
    @ApiOperation(value = "删除一个部门对象",notes = "根据id删除")
    @DeleteMapping("/{id}")
    public AjaxResult delect(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败！");
        }
    }

    @ApiOperation(value = "批量删除",notes = "")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            departmentService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败！");
        }
    }
}
